<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="css/mui.min.css">
		<link rel="stylesheet" type="text/css" href="css/app.css" />
		<link rel="stylesheet" type="text/css" href="css/subsetting.css" />
	</head>

	<body>
		<div class="mui-view" id="refreshContainer">
			<div class=" mui-content ">
				<div id="setting " class="mui-page-center ">
					<div class="mui-page-content">
						<div class="mui-scroll-wrapper">
							<div class="mui-scroll">
								<ul class="mui-table-view mui-table-view-chevron" id="mui-setting">
									<li class="mui-table-view-cell mui-media">
										<a>
											<img v-if="items.avatarUrl != null" class="mui-media-object mui-pull-left head-img " v-bind:src="items.avatarUrl">
											<img v-else class="mui-media-object mui-pull-left head-img " src="images/default.png">
											<div class="mui-media-body " class="mui-navigate-right">
												{{items.name}}
												<p class='mui-ellipsis'>手机号:{{items.mobile}}</p>
												<span class="mui-navigate-right"></span>
											</div>
										</a>
									</li>
								</ul>
								<div class="mui-content-padded">
									<p>会议预约展示、预约申请提交、预约取消</p>
								</div>
								<ul class="mui-table-view mui-table-view-chevron ">
									<li class="mui-table-view-cell ">
										<a href="javascript:;" id="bookingInfo" class="mui-navigate-right">我的预约</a>
									</li>
									<li class="mui-table-view-cell ">
										<a href="#javascript:;" id="my-files" class="mui-navigate-right ">我的文件</a>
									</li>
								</ul>
								<ul class="mui-table-view mui-table-view-chevron " id="messageInfo">
									<li class="mui-table-view-cell" id="msgInfo">
										<a href="javascript:;">新的消息
											<span v-show="total != 0" class="mui-badge mui-badge-danger"> {{total}} </span></a>
									</li>
								</ul>
								<div class="mui-content-padded">
									<p>用户账户密码修改</p>
								</div>
								<ul class="mui-table-view mui-table-view-chevron">
									<li class="mui-table-view-cell" id="modify">
										<a href="javascript" class="mui-navigate-right">密码修改</a>
									</li>
									<li class="mui-table-view-cell" id="checkVersion">
										<a href="javascript:;" class="mui-navigate-right">检查版本
										</a>
									</li>
								</ul>

								<!--<ul class="mui-table-view mui-table-view-chevron " id="messageInfo">
									<li class="mui-table-view-cell" id="msgInfo">
										<a href="javascript:;" class="mui-navigate-right">检查版本
											</a>
									</li>
								</ul>-->
								<ul class="mui-table-view mui-table-view-chevron ">
									<li class="mui-table-view-cell " id="delete">
										<a href="#delete" style="text-align: center;">退出登录</a>
									</li>
								</ul>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>

</html>
<script type="text/javascript" src="js/mui.min.js "></script>
<script type="text/javascript" src="js/vue.min.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/config.js"></script>
<script type="text/javascript" src="js/server.js"></script>
<script type="text/javascript">
	mui.init({
		pullRefresh: {
			container: "#refreshContainer", //下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
			down: {
				style: 'circle', //必选，下拉刷新样式，目前支持原生5+ ‘circle’ 样式
				color: '#2BD009', //可选，默认“#2BD009” 下拉刷新控件颜色
				height: '50px', //可选,默认50px.下拉刷新控件的高度,
				range: '100px', //可选 默认100px,控件可下拉拖拽的范围
				offset: '0px', //可选 默认0px,下拉刷新控件的起始位置
				auto: false, //可选,默认false.首次加载自动上拉刷新一次
				callback: pullrefresh //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
			}
		}
	});

	var readobj;
	var first = false;
	var wgtVer = null;

	//	用户登录对象
	var useLoginObj = {};
	//	消息中心数据对象
	var userUnreadMessagesObj = {};

	var logininfo = server.user.getUserInfo();
	var userinfo = server.user.getLoginUser();

	console.log('logininfo： ' + JSON.stringify(userinfo));

	initData();

	mui.plusReady(function() {

		window.addEventListener("myfresh", function(e) {
			pullrefresh();
		});

		//	资料修改
		mui('#mui-setting').on('tap', 'li a', function() {
			onPenView("setting.html", 'settingInfo');
		});

		//	我的预约
		mui('body').on('tap', '#bookingInfo', function() {
			onPenView("reservelist.html", 'bookingInfo');
		});

		// 我的文件
		mui('body').on('tap', '#my-files', function() {
			onPenView('my-files.html', 'myfiles');
		});

		//	我的消息
		mui('#messageInfo').on('tap', 'li a', function() {
			onPenView('message.html', 'messageInfo');
		});

		//	修改密码
		mui('#modify').on('tap', 'li a', function() {
			onPenView('modify_password.html', 'modify_password.html');
		});

		//	注销登录
		mui('#delete').on('tap', 'li a', function() {

			mui.confirm('确认注销登录', '友情提示', '', function(e) {
				if(e.index == 1) {
					var userLoginInfo = server.user.getUserInfo();
					localStorage.removeItem('logininfo');
					localStorage.removeItem('userinfo');
					if(userLoginInfo.userName) {
						// 让登陆页面刷新
						var page = plus.webview.getWebviewById('login.html');
						mui.fire(page, 'myfresh');
						// 请求服务器登出
						var waiting = plus.nativeUI.showWaiting('请稍后...', {
							back: 'close'
						});
						mui.post(config.getLogout + '?mobile=' + userLoginInfo.userName, function(r) {
							waiting.close();
							if(r.errCode == 0) {
								mui.openWindow({
									url: 'login.html',
									id: 'login.html',
									createNew: false, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
									show: {
										autoShow: true, //页面loaded事件发生后自动显示，默认为true
										aniShow: 'zoom-fade-out', //页面显示动画，默认为”slide-in-right“；
										duration: 500 //页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
									}
								});
								mui('#delete').popover('hide'); //隐藏注销按钮
							} else {
								mui.toast(r.errMsg);
							}
						});
					}
				}
			}, 'div');

		});

		//	版本检查
		mui('#checkVersion').on('tap', 'li a', function() {
			//	获取本地应用资源版本号
			plus.runtime.getProperty(plus.runtime.appid, function(info) {
				var appVer = info.version;
				var mtype = mui.os.android == true ? '1' : '2'; //plus.os.name 1,android 2,ios

				server.serverconfig.checkAppVersion(mtype, appVer, function(r) {
					console.log('r' + JSON.stringify(r));
					switch(r.errCode) {
						case 0:
							mui.confirm('是否下载新版本？', '温馨提示', '', function(e) {
								if(e.index == 1) {
									switch(plus.os.name) {
										case 'Android':

											//	查找更新下载APP目录是否有下载文件，存在则删除
											plus.io.resolveLocalFileSystemURL(
												'_downloads/update/',
												function(entry) {
													//	成功时回调：删除已存在的文档
													entry.removeRecursively(function() {
														//mui.toast('删除成功');
														console.log('删除当前目录相关文档');
													});
												},
												function(e) {
													console.log('当前目录暂无文档信息');
												}
											)

											plus.nativeUI.showWaiting("安装包下载中...");
											plus.downloader.createDownload(r.data.url, {
												filename: "_doc/update/"
											}, function(d, status) {
												if(status == 200) {
													console.log("下载apk文件成功：" + d.filename);
													installApp(d.filename); // 安装wgt包
												} else {
													console.log("下载apk文件失败！");
													plus.nativeUI.alert("下载apk文件失败！");
												}
												plus.nativeUI.closeWaiting();
											}).start();
											break;
										default:
											//mui.toast('下载IOS版本');	
											window.location.href = 'https://itunes.apple.com/cn/app/%E5%85%9A%E5%91%98%E7%9B%B4%E6%92%AD%E9%97%B4/id1384341559';
											break;
									}
								}
							}, 'div');
							break;
						case 4005:
							mui.toast(r.errMsg);
							break;
						default:
							break;
					}
				}, function(r) {
					mui.toast(r.errMsg);
				})

			});

		});

		//	安装新版本APP
		function installApp(path) {
			plus.nativeUI.showWaiting("APP安装中...");
			plus.runtime.install(path, {}, function() {
				plus.nativeUI.closeWaiting();
				console.log("APP安装成功！");
				plus.nativeUI.alert("应用资源更新完成！", function() {
					plus.runtime.restart();
				});
			}, function(e) {
				plus.nativeUI.closeWaiting();
				console.log("安装文件失败[" + e.code + "]：" + e.message);
				plus.nativeUI.alert("安装文件失败[" + e.code + "]：" + e.message);
			});
		}
	});

	//	初始化数据
	function initData() {

		//	判断本地是否有本地信息
		if(!logininfo) {
			app.getPageJump();
			return;
		}

		//	判断获取用户信息
		if(!userinfo) {
			app.getPageJump();
			return;
		}

		var userLogin = new Vue({
			el: "#mui-setting",
			data: useLoginObj = {
				items: userinfo
			}
		});

		getUnreadMsg();
	};

	//	打开新消息通知界面
	function onPenView(typeurl, typeid) {
		mui.openWindow({
			url: typeurl,
			id: typeid,
			/*		extras: {
						"meetingid": mid, //自定义扩展参数，可以用来处理页面间传值
						"userid": uid
					},*/
			createNew: false, //是否重复创建同样id的webview，默认为false:不重复创建，直接显示
			show: {
				autoShow: true, //页面loaded事件发生后自动显示，默认为true
				aniShow: 'slide-in-right', //页面显示动画，默认为”slide-in-right“；
				duration: 300 //页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
			},
			waiting: {
				autoShow: true, //自动显示等待框，默认为true
				title: '正在加载...' //等待对话框上显示的提示内容
			}
		})
	};

	//	下拉刷新功能
	function pullrefresh() {
		getLoginInfo();
		getUnreadMsg();
		setTimeout(function() {
			mui('#refreshContainer').pullRefresh().endPulldown();
			location.reload();
		}, 1000);
	}

	function getLoginInfo() {
		var userInfoobj = {
			"mobile": logininfo.userName
		}
		//	获取用户信息并存到本地
		server.user.setUserInfo(userInfoobj, function(r) {
			console.log('r:' + JSON.stringify(r.data));
			if(r.errCode == 0) {
				localStorage.setItem('userinfo', JSON.stringify(r.data));
			}
		}, function(r) {
			mui.toast("服务器错误，请稍后刷新重试");
		});
	}

	//	获取消息通知数据
	function getUnreadMsg() {

		//	消息中心参数
		var readobj = {
			userId: userinfo.id
		}
		server.message.getUnreadMsg(readobj, function(r) {
			if(!first) {
				userMessagesObj = {
					total: r
				}
				getDom();
				first = true;
			} else {
				userMessagesObj.total = r
			}
		}, function(r) {
			mui.toast("服务器错误，请稍后刷新重试");
		})

	};

	function getDom() {
		readobj = new Vue({
			el: "#msgInfo",
			data: userMessagesObj
		});
	}
</script>